package swing;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import logica.MenuAcao;

import pojo.Menu;
import pojo.MenuItem;
import pojo.Usuario;

public class MenuPrincipal extends JFrame {
	private JPanel painel1;
	private JLabel centro, usuario;
	private JMenuBar bar;
	private JMenu menu;
	private JMenuItem menuItem;
	private MenuAcao menuacao;
	private JDesktopPane theDesktop;
	private GridLayout grid;
    private Dimension screenSize;

	public MenuPrincipal(Usuario user){
        	super("Sistema especialiasta para a criação de cenários de produção");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            initialize();
            bar = new JMenuBar();
            setJMenuBar(bar);
            menuacao = new MenuAcao();
            criarMenu(user);
     //           centro.setText("Centro material: " + user.getCentro().getId() + "-" + user.getCentro().getDescricao());
                usuario.setText("Usuário: " + user.getNome());
	}

	/**
	 * This method initializes this
	 *
	 */
	private void initialize() {
            Font fonte = new Font("Serif", Font.BOLD, 20);
            grid = new GridLayout(2,1);
            painel1 = new JPanel();
            painel1.setSize(600, 40);
            painel1.setOpaque(false);
            usuario = new JLabel("");
            centro = new JLabel("");
            usuario.setFont(fonte);
            centro.setFont(fonte);
            painel1.add(centro);
            painel1.add(usuario);
            painel1.setLayout(grid);
            theDesktop = new JDesktopPane();
            theDesktop.add(painel1);
            add(theDesktop);
	}

	private void criarMenu(Usuario us){
		for (Menu m: us.getCategoria().getMenu()){
			 menu = new JMenu(m.getLabel());
			 for (final MenuItem i: m.getMenuitem()){
				 menuItem = new JMenuItem(i.getLabel());
				 menuItem.addActionListener(
			        		new ActionListener(){
			         			public void actionPerformed(ActionEvent event){
                                                        try {
                                                            if (menuacao.localizarAcao(i.getAcao()) == null) {
                                                        } else {
                                                                theDesktop.add(menuacao.localizarAcao(i.getAcao()));
                                                                theDesktop.selectFrame(true);
                                                               }
                                                        } catch (SQLException ex) {
                                                            Logger.getLogger(MenuPrincipal.class.getName()).log(Level.SEVERE, null, ex);
                                                        } catch (ClassNotFoundException ex) {
                                                            Logger.getLogger(MenuPrincipal.class.getName()).log(Level.SEVERE, null, ex);
                                                         }
                                                    }
                                                }
			        );
			 menu.add(menuItem);
			 }
			 bar.add(menu);
		}

		 menu = new JMenu("Sair");
                 menuItem = new JMenuItem("Sair do sistema");
		 menuItem.addActionListener(
	        		new ActionListener(){
	         			public void actionPerformed(ActionEvent event){
                                            
	         			   System.exit(0);
	       			}
	       		}
	        );
                menu.add(menuItem);
		 bar.add(menu);
	}

}
